/*This Project Impements the features of List in Python using Linked Lists of C*/
#include<stdio.h>
#include<stdlib.h>

struct node
   {
      int value;
      struct node *next;
   }; 


void disp(struct node *fir)
{
    struct node *p;
    printf("the list is : ");
    for(p=fir; p!=NULL; p=p->next)
    {
        printf("%d\n",p->value);
    }
   
}


struct node *enter(struct node *fir,int n)
{
    struct node *new_node;
    new_node=malloc(sizeof(struct node));
    if(new_node==NULL)
    {
       printf("THERE IS NO SPACE AVAILABLE :");
    }
    new_node->value=n;
         
    new_node->next=fir;
	fir=new_node;  
    return new_node;
}

void app(struct node *fir)
{
       int n;
       printf("enter the element u want to append :");
       scanf("%d",&n);
       fir=enter(fir,n);
       disp(fir);
    
}

struct node *ext(struct node *fir)
{
      int n,m,i;
      printf("enter the number of elements you want to extend :");
      scanf("%d",&m);
      printf("enter the elements : ");
      for(i=0;i<m;i++)
      {
          scanf("%d",&n);
          fir=enter(fir,n);
      }
      disp(fir);
      return fir;
}


struct node *po(struct node *fir,int n)
{
        struct node *p,*q;
        printf("enter the element u want to delete : ");
        scanf("%d",&n);
        p=NULL;
        q=fir;
        for(; q->next!=NULL && q->value!=n; p=q, q=q->next)
        {
           ;
        }
        if(q==NULL)    
        {
            return fir;
        }
        if(p==NULL)  
        {
           fir=fir->next;
        }
        else     
        { 
           p->next=q->next;
        }
        free(q);
        disp(fir);
        return fir;   
     }


struct node *ind(struct node *fir)
{
    int count=0,k;
    struct node *p;
    printf("enter the element whose index u want to find : ");
    scanf("%d",&k);
    for(p=fir;p!=NULL&&p->value!=k;p=p->next)
    {
       count=count+1;  
    }
    if(p==NULL)
    {
       printf("element not found");
    }
    if(p->value==k)
    {
       printf("the index of the element is  %d ",count-1);
    }
    return fir;
}

 
void ins(struct node *fir)
{
    struct node *q=fir,*r;
    int s,count=0,val,i;
    printf("enter the position where u want to insert : ");
    scanf("%d",&s);
    printf("enter the value you want to insert:");
    scanf("%d",&val);
	r=malloc(sizeof(struct node));
    
	r->value=val;
	for (i=1;i<s;i++)
        {	
            q=q->next;
	}	
        r->next=q->next;
	q->next=r;

	disp(fir);

}  


struct node *cou(struct node *fir)
{
    int count=1;
    struct node *p;
    for(p=fir;p->next!=NULL;p=p->next)
    {
       count=count+1;  
    }
    printf("number of elements is  %d ",count);
    return fir;
}

struct node *sor(struct node *fir)
{
	struct node *p, *q;
	int temp;
	for(p=fir;p!=NULL;p=p->next)
	{
		for(q=fir;q!=NULL;q=q->next)
		{
			if(p->value>q->value)
			{
				temp=p->value;	
				p->value=q->value;
				q->value=temp;
			}
		}
	}
        disp(fir);
        return fir; 	
}  

int main()
{
   int c,i,n,m;
   
   struct node *fir=NULL;
   printf("enter the number of elements u want to enter :");
   scanf("%d",&m);
  
   printf("enter the elements :");
   for(i=0;i<m;i++)
   {
       scanf("%d",&n);
       fir=enter(fir,n);
   }
   printf("1.app 2.ext 3.ins 4.ind 5.po 6.cou 7.sor 8.disp 9.delete ");
   scanf("%d",&c);
   switch(c)
   {
       case 1 :app(fir);
               break;
       case 2 :ext(fir);
               break;
       case 3 :ins(fir);
               break;
       case 4 :ind(fir);
               break;
       case 5 :po(fir);
               break;
       case 6 :cou(fir);
               break;
       case 7 :sor(fir);
                break; 
       case 8: disp(fir);
                break;
   }
   return 0; 
}

